package com.example.taobaounion.view.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.taobaounion.R;
import com.example.taobaounion.base.BaseFragment;
import com.example.taobaounion.manager.PresenterManager;
import com.example.taobaounion.model.domain.Categories;
import com.example.taobaounion.presenter.impl.HomePresenter;
import com.example.taobaounion.utils.LogUtils;
import com.example.taobaounion.view.activity.ScanQrCodeActivity;
import com.example.taobaounion.view.activity.impl.MainActivity;
import com.example.taobaounion.view.adapter.HomePagerAdapter;
import com.example.taobaounion.view.callback.IHomeCallback;
import com.google.android.material.tabs.TabLayout;

import butterknife.BindView;


public class HomeFragment extends BaseFragment {

    private static final String TAG = "HomeFragment";


    private TabLayout mTableLayout;

    private ViewPager homePager;

    private HomePresenter mHomePresenter;
    private IHomeCallback mIHomeCallback;
    private HomePagerAdapter mHomePagerAdapter;

    @BindView(R.id.home_search_input_box)
    public View mSearchInputBox;

    @BindView(R.id.scan_icon)
    public View mScanBtn;


    @Override
    protected void initVariable() {
        mTableLayout = mSuccessView.findViewById(R.id.home_indicator);
        homePager = mSuccessView.findViewById(R.id.home_pager);
    }


    @Override
    protected void initListener() {
        mScanBtn.setOnClickListener(v -> startActivity(new Intent(getContext(), ScanQrCodeActivity.class)));

        mSearchInputBox.setOnClickListener(v -> {
            // jump to search page
            FragmentActivity lActivity = getActivity();

            if (lActivity instanceof MainActivity) {
                ((MainActivity) lActivity).switch2SearchFragment();
            }
        });
    }

    @Override
    protected void initData() {

        setUpState(STATE.LOADING);

        mHomePresenter.getCategories();
    }

    @Override
    protected void initView(View view) {
        mTableLayout.setupWithViewPager(homePager);
        // 给homePager(ViewPager) 设置适配器
        mHomePagerAdapter = new HomePagerAdapter(getChildFragmentManager());

        homePager.setAdapter(mHomePagerAdapter);
    }

    @Override
    protected void initPresenter() {
        mHomePresenter = PresenterManager.getInstance().getHomePresenter();

        mIHomeCallback = new IHomeCallback() {
            @Override
            public void onCategoriesLoaded(Categories categories) {

                setUpState(STATE.SUCCESS);

                LogUtils.d(TAG, "onCategoriesLoaded...");

                if (mHomePagerAdapter != null) {
                    // 让控件一开始就加载那些数据， 此处为全部加载
//                    homePager.setOffscreenPageLimit(categories.getData().size());
                    mHomePagerAdapter.setCategories(categories);
                }
            }

            @Override
            public void onNetworkError() {
                setUpState(STATE.ERROR);
            }

            @Override
            public void onLoading() {
                setUpState(STATE.LOADING);
            }

            @Override
            public void onEmpty() {
                setUpState(STATE.EMPTY);
            }

        };
        mHomePresenter.registerViewCallback(mIHomeCallback);
    }

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_home;
    }


    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.base_home_fragment_layout, container, false);
    }

    @Override
    protected void release() {
        if (mHomePresenter != null) {
            mHomePresenter.unRegisterViewCallback(mIHomeCallback);
        }
    }

    @Override
    protected void onRetryClick() {

        LogUtils.d(TAG, "onRetryClick...");

        // 网络错误， 重新加载分类
        if (mHomePresenter != null) {
            mHomePresenter.getCategories();
        }
    }
}
